package org.example.hansp.thread;

public class ThreadProxyDemo {
    public static void main(String[] args) throws InterruptedException {

      /*  Cat cat = new Cat();
        ThreadProxy proxy = new ThreadProxy(cat);
        proxy.start();*/
        Cat cat = new Cat();
        Thread thread1 = new Thread(cat);
        thread1.start();
        Dog dog = new Dog();
        Thread thread = new Thread(dog);
        thread.start();
        System.out.println("程序继续执行");
        for (int i = 0; i < 10; i++) {
            System.out.println("main"+i);
            Thread.sleep(10);
        }
    }
}
class Animal{}
class Cat extends Animal implements Runnable{
    @Override
    public void run() {
        int count=0;
        while(true){
            System.out.println("喵喵喵~"+(++count));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if(count==10){
                break;
            }
        }
    }
}
class Dog extends Animal implements Runnable{

    @Override
    public void run() {
        int count=0;
        while(true){
            System.out.println("汪汪汪"+(++count));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if(count==10){
                break;
            }
        }
    }
}
//模拟线程的代理模式，极简
class ThreadProxy implements Runnable{
private Runnable target;
    @Override
    public void run() {
        if(target!=null){
            target.run();
        }
    }
    public ThreadProxy(Runnable target){
        this.target=target;
    }
    public void start(){
        start0();
    }
    public void start0(){
        run();
    }

}